//
//  CommonInputView.m
//  fitnessmanage
//
//  Created by 众拳科技 on 2017/10/23.
//  Copyright © 2017年 zongquankeji. All rights reserved.
//

#import "CommonInputView.h"

@implementation CommonInputView

-(id)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        self.backgroundColor = CSWhiteColor;
        
        UILabel *titleLabel = [[UILabel alloc] init];
        //titleLabel.text = @"地址*";
        titleLabel.font = MidBigFont;
        titleLabel.textColor = CSColorFromRGB(0x666666);
        [self addSubview:titleLabel];
        self.titleLabel = titleLabel;
        
        /**
         场馆地址
         */
        UITextField *titleTF = [[UITextField alloc] init];
        UIView *addrLeftView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 5, 15)];
        titleTF.leftView = addrLeftView1;
        titleTF.font = MidBigFont;
        titleTF.textColor = TFColor;
        //titleTF.backgroundColor = CSRedColor;
        titleTF.leftViewMode = UITextFieldViewModeAlways;
        titleTF.clearButtonMode=UITextFieldViewModeWhileEditing;
        titleTF.textAlignment = NSTextAlignmentRight;
        //        brandNameTF.backgroundColor = CSRedColor;
        [self addSubview:titleTF];
        self.titleTF = titleTF;;
        
        UIView *lineView= [[UIView alloc] init];
        lineView.backgroundColor = CSColorFromRGB(0xe6e6e6);
        [self addSubview:lineView];
        self.lineView = lineView;;
        
        [titleLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew context:nil];
        
        [titleLabel addObserver:self forKeyPath:@"placeholder" options:NSKeyValueObservingOptionNew context:nil];
        
        [self setConstraints];
    }
    
    return self;
}

-(void)setConstraints{
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.titleTF);
        
        make.left.mas_equalTo(self).offset(10);
        
        make.width.mas_equalTo(100);
    }];
    
    
    [self.titleTF mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.top.bottom.mas_equalTo(self);
        
        make.right.mas_equalTo(self).offset(-15);
        
        make.left.mas_equalTo(self.titleLabel.mas_right);
        
        make.height.mas_equalTo(60);
    }];
    
    [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {

        make.bottom.mas_equalTo(self);

        make.left.mas_equalTo(self).offset(10);

        make.right.mas_equalTo(self);

        make.height.mas_equalTo(1);
    }];
}


- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
    
    if ([keyPath isEqualToString:@"text"]) {
     
        [self strWithRed];
        
        CGSize titleSize = [self.titleLabel.text sizeWithFont:MidFont maxSize:CGSizeMake(MAXFLOAT, 30)];
        
        if (titleSize.width >= 90) {
         
            [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                
                make.centerY.mas_equalTo(self.titleTF);
                
                make.left.mas_equalTo(self).offset(10);
                
                make.width.mas_equalTo(titleSize.width + 40);
            }];
        }
    }
    else{
        
        NSMutableDictionary *attrs = [NSMutableDictionary dictionary];// 创建属性字典
        attrs[NSFontAttributeName] = MidBigFont; // 设置font
        attrs[NSForegroundColorAttributeName] = PlaceHoldColor; // 设置颜色
        NSString *newStr = change[NSKeyValueChangeNewKey];
        NSAttributedString *phoneAttStr = [[NSAttributedString alloc] initWithString:newStr attributes:attrs]; // 初始化富文本占位字符串
        self.titleTF.attributedPlaceholder = phoneAttStr;
    }
}


-(void)strWithRed{
    
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.titleLabel.text];
    
    [str addAttribute:NSForegroundColorAttributeName value:CSRedColor range:[self.titleLabel.text rangeOfString:@"*"]];
    
    self.titleLabel.attributedText = str;
}

-(void)setShowDownImage:(BOOL)showDownImage{
    
    _showDownImage = showDownImage;
    
    UIImageView *downImageView = [[UIImageView alloc] init];
    downImageView.image = [UIImage imageNamed:@"formlist_icon_d_arrow"];
    [self addSubview:downImageView];
    self.downImageView = downImageView;
    CGSize titleSize = [self.titleLabel.text sizeWithFont:MidFont maxSize:CGSizeMake(MAXFLOAT, 30)];
    [downImageView mas_remakeConstraints:^(MASConstraintMaker *make) {
       
        make.centerY.mas_equalTo(self.titleLabel);
        make.left.mas_equalTo(self).offset(10 + titleSize.width + 5);
    }];
}

-(void)dealloc{
    
    [self.titleLabel removeObserver:self forKeyPath:@"text"];
    [self.titleLabel removeObserver:self forKeyPath:@"placeholder"];
    self.titleLabel = nil;
}
@end
